<template>
  <div>
    <el-table
      :data="list"
      style="width: 100%"
      row-key="id"
      height="73vh"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="kind_name" label="名称" width>
        <template slot-scope="scope">
          <span>{{ scope.row.kind_name }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="style" label="图片" width>
        <template slot-scope="scope">
          <el-avatar :size="60" :src="scope.row.style" shape="square" />
        </template>
      </el-table-column>
      <!-- <el-table-column prop="isShow" label="是否首页展示" width>
        <template slot-scope="scope">
          <div>
            <el-switch v-model="scope.row.isShow" />
          </div>
        </template>
      </el-table-column> -->
      <el-table-column prop="sort" label="排序" width>
        <template slot-scope="scope">
          <div class="d-flex">
            <el-input
              ref="four"
              v-model="scope.row.sort"
              :disabled="true"
              class="w-60 input_sort"
              @blur="handleBlur(scope, 'input_sort')"
            />
            <i class="el-icon-edit fs" @click.stop="iconClick(scope.$index, 'input_sort')" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <span class="edit" @click="edit(scope)">编辑</span>
          <span v-if="scope.row.children" class="add" @click="addChildren(scope)">添加子分类</span>
          <span class="del" @click="del(scope)">删除</span>
        </template>
      </el-table-column>
    </el-table>
    <Add ref="add" :list="list" :option="option" :list-item="listItem" @confirm="handleConfirm" />
  </div>
</template>
<script>
import Add from './add'
import { setCategorySort, addOrEditCategoryItem, deleteCategoryItem } from '@/api/category'
const ERR_OK = 200
export default {
  components: {
    Add
  },
  props: {
    list: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      option: '',
      listItem: {}
    }
  },
  methods: {
    iconClick(index, name) {
      const el = document.querySelectorAll(`.${name}`)[index].children[0]
      el.style.background = '#eee'
      el.removeAttribute('disabled')
      el.focus()
    },
    handleBlur(scope, name) {
      const index = scope.$index
      const row = scope.row
      const el = document.querySelectorAll(`.${name}`)[index].children[0]
      el.style.background = 'transparent'
      el.setAttribute('disabled', 'disabled')
      this._setCategorySort({ id: row.id, sort: row.sort })
    },
    addChildren(scope) {
      this.$refs.add.show()
      this.option = 'addChildren'
      this.listItem = scope.row
    },
    add() {
      this.$refs.add.show()
      this.option = 'add'
    },
    edit(scope) {
      this.$refs.add.show()
      console.log(scope)
      this.listItem = scope.row
      this.option = 'edit'
    },
    del(scope) {
      this._deleteCategoryItem(scope.row.id)
        .then((res) => {
          this.$emit('deleteItem', scope)
        })
        .catch((err) => {
          console.log(err)
        })
    },
    handleConfirm(params) {
      console.log(params)
      switch (this.option) {
        case 'addChildren':
          break
        case 'add':
          if (!params.higherup) {
            params.higherup = 0
          }
          break
        case 'edit':
          if (this.listItem.children) {
            params.higherup = 0
          }
          break
      }
      this._addOrEditCategoryItem(params)
        .then(() => {
          this.$emit('confirm')
        })
        .catch(err => {
          console.log(err)
        })
    },
    _setCategorySort({ id, sort }) {
      setCategorySort({ id, sort })
        .then(res => {
          if (res.code === ERR_OK) {
            console.log(res)
            this._success(res)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    _addOrEditCategoryItem({ id, kind_name, higherup, style }) {
      return new Promise((resolve, reject) => {
        addOrEditCategoryItem({ id, kind_name, higherup, style })
          .then(res => {
            if (res.code === ERR_OK) {
              console.log(res)
            }
            this._success(res)
            resolve()
          })
          .catch(err => {
            console.log(err)
            reject(err)
          })
      })
    },
    _deleteCategoryItem(id) {
      return new Promise((resolve, reject) => {
        deleteCategoryItem(id)
          .then(res => {
            if (res.code === ERR_OK) {
              console.log(res)
              this._success(res)
            }
            resolve(res)
          })
          .catch(err => {
            console.log(err)
            reject(err)
          })
      })
    },
    _success(res) {
      this.$message({
        showClose: true,
        message: res.message,
        type: 'success'
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "@/styles/el-table.scss";
.el-table /deep/ .cell {
  .edit, .add {
    color: #409eff;
    margin-right: 24px;
    cursor: pointer;
    white-space: nowrap;
  }
  .del {
    color: #f56c6c;
    cursor: pointer;
    white-space: nowrap;
  }
}
.el-table /deep/ .cell {
  .el-avatar {
    margin-right: 15px;
  }
}
.d-flex {
  display: flex !important;
  align-items: center;
  justify-content: center;
}
.w-60 {
  width: 40px;
}
.el-input {
  display: inline-block;
}
.el-input /deep/ .el-input__inner {
  background-color: transparent;
  border-color: transparent;
  color: #666666;
  border: none;
  height: auto;
  line-height: 0;
  padding: 0;
  text-align: center;
  white-space: nowrap;
  cursor: pointer;
}
.fs {
  font-size: 20px;
  color: #409eff;
  cursor: pointer;
  opacity: 0;
  transform: all 1s;
}
.el-table /deep/ .el-table__row:hover {
  .fs {
    opacity: 1;
  }
}
</style>
